package atom.core3.spring.web.exception;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import atom.core0.exception.BizException;
import atom.core0.exception.BizOperException;
import atom.core0.exception.ExceptionUtil;
import atom.core0.global.GlobalCache;
import atom.core0.util.StringUtil;
  
public class ExceptionHandler implements HandlerExceptionResolver {  
	protected static Logger logger = Logger.getLogger(ExceptionHandler.class);
 
    public ModelAndView resolveException(HttpServletRequest request,  
            HttpServletResponse response, Object handler, Exception ex) {  
        // TODO Auto-generated method stub  
    	if (ex instanceof BizException)
    		logger.warn(ex.getMessage(),ex);
    	else
    		logger.error(ex.getMessage(),ex);
    	
    	BizException e = ExceptionUtil.convert(ex);
    	request.setAttribute("e", e);
    	
    	//可以继承 自己写异常处理分支
    	if (e instanceof BizOperException)
    	{
	    	if (StringUtil.isEmpty(GlobalCache.getGlobalOperErrorUrl()))
	    		return new ModelAndView("/pages_public/common/exception");
	    	else
	    		return new ModelAndView(GlobalCache.getGlobalOperErrorUrl());  
    	}
    	else
    	{
	    	if (StringUtil.isEmpty(GlobalCache.getGlobalErrorUrl()))
	    		return new ModelAndView("/pages_public/common/exception");
	    	else
	    		return new ModelAndView(GlobalCache.getGlobalErrorUrl());  
	    	}
    }  
  
}  
